package com.anyou.mall.parts.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.anyou.common.core.domain.ApiResult;
import com.anyou.common.utils.DateUtils;
import com.anyou.mall.parts.domain.PlaPartsClassifyFee;
import com.anyou.mall.parts.domain.PlaPartsClassifyParam;
import com.anyou.mall.parts.mapper.PlaPartsClassifyFeeMapper;
import com.anyou.mall.parts.mapper.PlaPartsClassifyParamMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.anyou.mall.parts.mapper.PlaPartsClassifyMapper;
import com.anyou.mall.parts.domain.PlaPartsClassify;
import com.anyou.mall.parts.service.IPlaPartsClassifyService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 配件分类Service业务层处理
 *
 * @author ylj
 * @date 2022-03-09
 */
@Service
public class PlaPartsClassifyServiceImpl extends ServiceImpl<PlaPartsClassifyMapper, PlaPartsClassify> implements IPlaPartsClassifyService
{
    @Autowired
    private PlaPartsClassifyMapper plaPartsClassifyMapper;

    @Autowired
    private PlaPartsClassifyParamMapper plaPartsClassifyParamMapper;

    @Autowired
    private PlaPartsClassifyFeeMapper plaPartsClassifyFeeMapper;

    @Autowired
    private PlaPartsClassifyParamServiceImpl plaPartsClassifyParamService;

    /**
     * 查询配件分类
     *
     * @param partsClassifyId 配件分类主键
     * @return 配件分类
     */
    @Override
    public PlaPartsClassify selectPlaPartsClassifyByPartsClassifyId(Long partsClassifyId)
    {
        return plaPartsClassifyMapper.selectPlaPartsClassifyByPartsClassifyId(partsClassifyId);
    }

    /**
     * 查询配件分类列表
     *
     * @param plaPartsClassify 配件分类
     * @return 配件分类
     */
    @Override
    public List<PlaPartsClassify> selectPlaPartsClassifyList(PlaPartsClassify plaPartsClassify)
    {
        //前端封装tree
        return plaPartsClassifyMapper.selectPlaPartsClassifyList(plaPartsClassify);
    }

    /**
     * 新增配件分类
     *
     * @param plaPartsClassify 配件分类
     * @return 结果
     */
    @Override
    public int insertPlaPartsClassify(PlaPartsClassify plaPartsClassify)
    {
        if(plaPartsClassify.getParentId() != null){
            PlaPartsClassify parentClassify = plaPartsClassifyMapper.selectById(plaPartsClassify.getParentId());
            if(parentClassify != null){
                plaPartsClassify.setLevel(parentClassify.getLevel() + 1);
            }
        }
        plaPartsClassify.setCreateTime(DateUtils.getNowDate());
        return plaPartsClassifyMapper.insertPlaPartsClassify(plaPartsClassify);
    }

    /**
     * 修改配件分类
     *
     * @param plaPartsClassify 配件分类
     * @return 结果
     */
    @Override
    public int updatePlaPartsClassify(PlaPartsClassify plaPartsClassify)
    {
        plaPartsClassify.setUpdateTime(DateUtils.getNowDate());
        return plaPartsClassifyMapper.updatePlaPartsClassify(plaPartsClassify);
    }

    /**
     * 批量删除配件分类
     *
     * @param partsClassifyIds 需要删除的配件分类主键
     * @return 结果
     */
    @Override
    public int deletePlaPartsClassifyByPartsClassifyIds(Long[] partsClassifyIds)
    {
        return plaPartsClassifyMapper.deletePlaPartsClassifyByPartsClassifyIds(partsClassifyIds);
    }

    /**
     * 删除配件分类信息
     *
     * @param partsClassifyId 配件分类主键
     * @return 结果
     */
    @Override
    public int deletePlaPartsClassifyByPartsClassifyId(Long partsClassifyId)
    {
        return plaPartsClassifyMapper.deletePlaPartsClassifyByPartsClassifyId(partsClassifyId);
    }

    @Override
    public ApiResult getParam(Long partsClassifyId) {
        PlaPartsClassify classify = plaPartsClassifyMapper.selectById(partsClassifyId);
        if(classify == null){
            return ApiResult.error("配件分类编号错误或不存在");
        }else{
            if(classify.getLevel() != 3){
                return ApiResult.error("三级分类无固定参数");
            }
        }
        List<String> list = plaPartsClassifyParamMapper.getParamByClassifyId(partsClassifyId);
        return ApiResult.success(list);
    }

    @Override
    @Transactional
    public ApiResult bindingParam(Long partsClassifyId, List<String> paramList) {
        PlaPartsClassify classify = plaPartsClassifyMapper.selectById(partsClassifyId);
        if(classify == null){
            return ApiResult.error("配件分类编号错误或不存在");
        }else{
            if(classify.getLevel() != 3){
                return ApiResult.error("三级分类不能绑定固定参数");
            }
        }
        plaPartsClassifyParamMapper.delByClassifyId(partsClassifyId);
        List<PlaPartsClassifyParam> list = new ArrayList<>();
        for (String paramName : paramList) {
            PlaPartsClassifyParam classifyParam = new PlaPartsClassifyParam();
            classifyParam.setParamName(paramName);
            classifyParam.setPartsClassifyId(partsClassifyId);
            list.add(classifyParam);
        }
        plaPartsClassifyParamService.saveBatch(list);
        return ApiResult.success();
    }

    @Override
    public ApiResult changeIsShow(Long partsClassifyId, String isShow) {
        PlaPartsClassify plaPartsClassify = plaPartsClassifyMapper.selectById(partsClassifyId);
        if(plaPartsClassify != null){
            plaPartsClassify.setIsShow(isShow);
            plaPartsClassifyMapper.updateById(plaPartsClassify);
            return ApiResult.success();
        }else{
            return ApiResult.error("配件分类编号错误或不存在");
        }
    }

    @Override
    public ApiResult getFee(Long partsClassifyId) {
        PlaPartsClassify classify = plaPartsClassifyMapper.selectById(partsClassifyId);
        if(classify == null){
            return ApiResult.error("配件分类编号错误或不存在");
        }else{
            if(classify.getLevel() != 3){
                return ApiResult.error("三级分类无配送费一口价");
            }
        }
        PlaPartsClassifyFee plaPartsClassifyFee = plaPartsClassifyFeeMapper.selectOne(new LambdaQueryWrapper<PlaPartsClassifyFee>().eq(PlaPartsClassifyFee::getPartsClassifyId, partsClassifyId).eq(PlaPartsClassifyFee::getDelFlag, '0'));
        return ApiResult.success(plaPartsClassifyFee == null ? BigDecimal.ZERO : plaPartsClassifyFee.getPrice());
    }

    @Override
    public ApiResult setFee(Long partsClassifyId, BigDecimal price) {
        PlaPartsClassify classify = plaPartsClassifyMapper.selectById(partsClassifyId);
        if(classify == null){
            return ApiResult.error("配件分类编号错误或不存在");
        }else{
            if(classify.getLevel() != 3){
                return ApiResult.error("三级分类不能设置配送费一口价");
            }
        }
        PlaPartsClassifyFee plaPartsClassifyFee = plaPartsClassifyFeeMapper.selectOne(new LambdaQueryWrapper<PlaPartsClassifyFee>().eq(PlaPartsClassifyFee::getPartsClassifyId, partsClassifyId).eq(PlaPartsClassifyFee::getDelFlag, '0'));
        if(plaPartsClassifyFee == null){
            plaPartsClassifyFee = new PlaPartsClassifyFee();
            plaPartsClassifyFee.setPartsClassifyId(partsClassifyId);
            plaPartsClassifyFee.setPrice(price);
            plaPartsClassifyFeeMapper.insert(plaPartsClassifyFee);
        }else{
            plaPartsClassifyFee.setPrice(price);
            plaPartsClassifyFeeMapper.updateById(plaPartsClassifyFee);
        }
        return ApiResult.success();
    }
}
